home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Eagles Nest BBS 8
/
Eagles_Nest_Mac_Collection_Disc_8.TOAST
/
Developer Tools⁄Additions
/
IntermediatC
/
Ships #6
/
teacher's compute.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-10-25
|
2KB
|
76 lines
/*
COMPUTE.C
Ships at Sea:
Create 25 ship locations (x,y) between -100 and 100 for each x and y. Determine
which 2 of the 25 ships are closest together. Print the locations of all ships
and the numbers of the 2 closest ships. Three modules must be used:
control.c contains main()
compute.c creates the ship locations and locates the 2 closest ships
output.c prints data to disk
*/
/*
This is compute.c. This module will create a list of ship
locations and determine the closest pair.
*/
#include <stdlib.h> /* used for rand() declaration */
#define square_of(x) ((x) * (x))
#define random(a,b) (rand() % ((b) - (a)) + (a))
#define LOW_VALUE -100
#define HIGH_VALUE 100
void create_locations
(double latitudes[], double longitudes[], int no_of_ships);
void locate_nearest_pair
(int nearest_subscripts[], double latitudes[], double longitudes[],
int no_of_ships);
void create_locations
(double latitudes[], double longitudes[], int no_of_ships)
{
int i;
for (i = 0; i < no_of_ships; i++)
{
latitudes[i] = random(LOW_VALUE, HIGH_VALUE);
longitudes[i] = random(LOW_VALUE, HIGH_VALUE);
}
}
void locate_nearest_pair
(int nearest_subscripts[], double latitudes[], double longitudes[],
int no_of_ships)
{
int i, j;
double distance_squared, new_distance;
distance_squared = HIGH_VALUE - LOW_VALUE;
distance_squared = 2.0 * square_of(distance_squared);
for (i = 0; i < no_of_ships; i++)
for (j = i + 1; j < no_of_ships; j++)
{
new_distance = square_of(latitudes[i] - latitudes[j])
+ square_of(longitudes[i] - longitudes[j]);
if (new_distance < distance_squared)
{
distance_squared = new_distance;
nearest_subscripts[0] = i;
nearest_subscripts[1] = j;
}
}
}